package vn.khanhhoi.controller;

import vn.khanhhoi.plugins.sms.SmsManage;
import vn.khanhhoi.plugins.tracking.TrackingPlugin;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ViewResolver;
import vn.khanhhoi.*;
import vn.khanhhoi.persistent.models.IPlugin;
import com.fasterxml.jackson.annotation.JsonRawValue;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.RequestParam;
import vn.khanhhoi.YuiCompressor.Options;
import vn.khanhhoi.appmanage.AppManage;
import vn.khanhhoi.plugins.defaultPage.Aboutus;
import vn.khanhhoi.plugins.defaultPage.Register;

@Controller
public class HomeController {

    private List<IPlugin> lsPlugin;
    @Autowired
    private ViewResolver viewResolver;

    @RequestMapping("/dashboard")
    public ModelAndView Dashboard(@RequestParam(value = "plugin", required = false) String plugin) {
        //load all plugin
        TrackingPlugin tracker = new TrackingPlugin();
        SmsManage sms = new SmsManage();
        AppManage app = new AppManage();
        Aboutus aboutus = new Aboutus();
        Register register=new Register();
        lsPlugin = new ArrayList<IPlugin>();
        lsPlugin.add(tracker);
        lsPlugin.add(sms);
        lsPlugin.add(app);
        lsPlugin.add(aboutus);
        lsPlugin.add(register);
        String message = "";
        for (Iterator<IPlugin> p = lsPlugin.iterator(); p.hasNext();) {
            IPlugin item = p.next();
            message = message + item.PresentationName();
        }
        if(plugin!=null&&!plugin.isEmpty()){
            //Shound return to view base on html
            return new ModelAndView("templates/Wee/index");
        }
        //return new ModelAndView("templates/Wee/index", "message", message);  
        return new ModelAndView("templates/Wee/index");
    }

    @RequestMapping(value = "/render", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public @JsonRawValue
    String Render(String plugin, String strView, HttpServletRequest request, HttpServletResponse response) {
        String view;
        if (strView == null || strView == "") {
            view = "plugins/" + plugin + "/index";
        } else {
            view = strView;
        }
        ModelMap model = new RenderModel().GetModel();
        String html = new RenderToString().renderViewToString(model, request, response, view, viewResolver);
        Document doc = Jsoup.parse(html);
        Elements scriptElements = doc.getElementsByTag("script");
        String script = "";
        String style = "";
        if (!scriptElements.isEmpty()) {
            Elements elScript = scriptElements.remove();
            script = Utility.EncodeHtml(YuiCompressor.compressJavaScript(elScript.html(), new Options()));
                //elScript.get(0).html()

        }
        Elements styleElements = doc.getElementsByTag("link");
        if (!styleElements.isEmpty()) {
            Elements elScript = styleElements.remove();
            style = Utility.EncodeHtml(elScript.outerHtml());
            //elScript.get(0).html()
        }
        html = Utility.EncodeHtml(doc.body().html());

        return "{\"HTML\": \"" + html + "\", \"SCRIPT\": \"" + script + "\", \"PLUGIN\": \"" + plugin + "\",\"STYLE\": \"" + style + "\"}";

    }

    @RequestMapping(value = "/menu", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public @JsonRawValue
    String Menu(HttpServletRequest request, HttpServletResponse response) {
        String menu = new RenderMenu().Render(lsPlugin);
        String encodeHtml = menu.replace("\"", "\\\"").replaceAll("\n\n", "");
        return "{\"Menu\": \"" + encodeHtml + "\"}";

    }

    @RequestMapping(value = "/getdata", method = RequestMethod.GET)
    @ResponseBody
    public @JsonRawValue
    String GetData() {
        String jsonData = "{\"HTML\": \"abc\", \"SCRIPT\": \"CreateNewDoc()\"}";
        return jsonData;
    }

}
